<?php

class PotentialactivitiesController extends AppController
{

    var $name = 'Potentialactivities';
    var $components = array('Email');
    var $helpers = array('Time');

    function index()
    {
        $this->Potentialactivity->recursive = 0;
        $this->set('potentialactivities', $this->paginate());
    }

    function view($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid activity', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('potentialactivity', $this->Potentialactivity->read(null, $id));
    }

    function add()
    {
        if( !empty($this->data) )
        {
            $this->Potentialactivity->create();

            /* use saveAll to save all the relationships too in the related tables */
            if( $this->Potentialactivity->saveAll($this->data) )
            {
                $this->Session->setFlash(__('The activity has been saved', true), 'success');
                $this->redirect(array('controller' => 'potentials', 'action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The activity could not be saved. Please, try again.', true), 'notice');
            }
        }

        if( !empty($this->passedArgs['forpotential']) && isset($this->passedArgs['forpotential']) )
        {
            $potentials = $this->Potentialactivity->Potential->find('first', array(
                'conditions' => array(
                    'Potential.id' => $this->passedArgs['forpotential']
                )
            ));

            $activitytypes = $this->Potentialactivity->Activitytype->find('list');

            $assignedtoemployees = $this->Potentialactivity->Employee->find('list', array(
                'conditions' => array(
                    'Employee.id !=' => Configure::read('employeetype.admin.id')
                )
            ));

            $this->set(compact('potentials', 'activitytypes', 'assignedtoemployees'));
        }
        else
        {
            $this->Session->setFlash(__('Please select the user first', true), 'notice');
            $this->redirect(array('controller' => 'potentials', 'action' => 'index'));
        }
    }

    function edit($id = null)
    {
        if( !$id && empty($this->data) )
        {
            $this->Session->setFlash(__('Invalid activity', true));
            $this->redirect(array('action' => 'index'));
        }
        if( !empty($this->data) )
        {
            if( $this->Potentialactivity->saveAll($this->data) )
            {
                $this->Session->setFlash(__('The activity has been edited successfully', true), 'success');
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The activity could not be saved. Please, try again.', true), 'notice');
            }
        }
        if( empty($this->data) )
        {
            $this->data = $this->Potentialactivity->find('first', array(
                'conditions' => array(
                    'Potentialactivity.id' => $id
                ),
                'contain' => array(
                    'Potential' => array(
                        'fields' => array('id'),
                        'User' => array(
                            'fields' => array('id', 'firstname', 'lastname')
                        )
                    )
                )
            ));
        }
        $potentials = $this->Potentialactivity->Potential->find('list');
        $activitytypes = $this->Potentialactivity->Activitytype->find('list');
        $employees = $this->Potentialactivity->Employee->find('list');
        $assignedtoemployees = $this->Potentialactivity->Employee->find('list');
        $documents = $this->Potentialactivity->Document->find('list');
        $this->set(compact('potentials', 'activitytypes', 'employees', 'assignedtoemployees', 'documents'));
    }

    function delete($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid id for activity', true));
            $this->redirect(array('action' => 'index'));
        }
        if( $this->Potentialactivity->delete($id) )
        {
            $this->Session->setFlash(__('Activity deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Activity was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    /**
     * Shows up the form for sending an email
     *
     * @param Int $to The ID of the potential for sending an email to
     * @todo currently the email functionality is for potentials only. We need to add
     * the email functionality for users also.
     */
    function email($id = null)
    {
        if( !$id && empty($this->data) )
        {
            $this->Session->setFlash(__('Invalid activity performed. Please try again.', true), 'notice');
            $this->redirect(array('controller' => 'potentials', 'action' => 'index'));
        }

        if( !empty($this->data) )
        {
            $potentialdata = $this->Potentialactivity->Potential->find('first', array(
                'conditions' => array(
                    'Potential.id' => $this->data['Potentialactivity']['potential_id']
                ),
                'contain' => array(
                    'User' => array(
                        'fields' => array('id', 'firstname', 'lastname', 'email1')
                    )
                )
            ));
            $this->Potentialactivity->create();

            /* use saveAll to save all the relationships too in the related tables */
            if( $this->Potentialactivity->saveAll($this->data) )
            {
                if( !empty($this->data['Document']) )
                {
                    $attachments = array();
                    foreach($this->data['Document']['Document'] as $docid)
                    {
                        $attachment = $this->Potentialactivity->Document->find('first', array(
                            'conditions' => array(
                                'Document.id' => $docid
                            ),
                            'fields' => array('id', 'name', 'filename'),
                            'recursive' => -1
                        ));
                        $attachments[] = DOCUMENTS_PATH . $attachment['Document']['filename'];
                    }
                }
                Configure::load('emailsettings');
                if( $this->_sendEmail
                (
                'Testing with sending email to potentials',
                $potentialdata['User']['email1'],
                Configure::read('smtp.displayname'),
                $this->data['Potentialactivity']['subject'],
                Configure::read('smtp.email'),
                $attachments,
                $this->data['Potentialactivity']['description']
                ) )
                {
                    $this->Session->setFlash('Email sent successfully', 'success');
                    $this->redirect(array('controller' => 'potentials', 'action' => 'index', 'by' => 'leads', 'show' => 'all'));
                }
                else
                {
                    $this->Session->setFlash('There was a problem sending Email. Please try again later.', 'notice');
                    $this->redirect(array('controller' => 'potentialactivities', 'action' => 'email', $this->data['Potentialactivity']['potential_id']));
                }
            }
            else
            {
                $this->Session->setFlash('There was a problem saving your activity. Please try again later.', 'notice');
                $this->redirect(array('controller' => 'potentialactivities', 'action' => 'email', $this->data['Potentialactivity']['potential_id']));
            }
        }

        $potentialinfo = $this->Potentialactivity->Potential->find('first', array(
            'conditions' => array(
                'Potential.id' => $id
            ),
            'link' => array(
                'User' => array(
                    'fields' => array('id', 'firstname', 'lastname', 'email1')
                )
            )
        ));
        $documents = $this->Potentialactivity->Document->find('list');
        $documentinfo = $this->Potentialactivity->Document->find('all');
        $this->set(compact('potentialinfo', 'documents', 'documentinfo'));
    }

    /**
     * Shows the assigned activities of current logged in user only
     */
    function assigned()
    {
        $this->paginate['Potentialactivity'] = array(
            'conditions' => array(
                'Potentialactivity.assignedtoemployee_id' => $this->Session->read('Auth.Employee.id')
            ),
            'fields' => array('id', 'duedate', 'nextfollowupdate', 'donedate', 'finalstatus', 'potential_id'),
            'contain' => array(
                'Employee' => array(
                    'fields' => array('id', 'firstname', 'lastname', 'primaryemail')
                ),
                'Activitytype' => array(
                    'fields' => array('id', 'name')
                ),
                'Assignedtoemployee' => array(
                    'fields' => array('id', 'firstname', 'lastname', 'primaryemail')
                ),
                'Potential' => array(
                    'fields' => array('id', 'user_id'),
                    'User' => array(
                        'fields' => array('id', 'firstname', 'lastname')
                    )
                )
            )
        );
        $activities = $this->paginate('Potentialactivity');
        $this->set('activities', $activities);
    }

    function admin_index()
    {
        $this->Potentialactivity->recursive = 0;
        $this->set('potentialactivities', $this->paginate());
    }

    function admin_view($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid activity', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('potentialactivity', $this->Potentialactivity->read(null, $id));
    }

    function admin_add()
    {
        if( !empty($this->data) )
        {
            $this->Potentialactivity->create();
            if( $this->Potentialactivity->save($this->data) )
            {
                $this->Session->setFlash(__('The activity has been saved', true));
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The activity could not be saved. Please, try again.', true));
            }
        }
        $potentials = $this->Potentialactivity->Potential->find('list');
        $activitytypes = $this->Potentialactivity->Activitytype->find('list');
        $employees = $this->Potentialactivity->Employee->find('list');
        $assignedtoemployees = $this->Potentialactivity->Employee->find('list');
        $documents = $this->Potentialactivity->Document->find('list');
        $this->set(compact('potentials', 'activitytypes', 'employees', 'assignedtoemployees', 'documents'));
    }

    function admin_edit($id = null)
    {
        if( !$id && empty($this->data) )
        {
            $this->Session->setFlash(__('Invalid activity', true));
            $this->redirect(array('action' => 'index'));
        }
        if( !empty($this->data) )
        {
            if( $this->Potentialactivity->save($this->data) )
            {
                $this->Session->setFlash(__('The activity has been saved', true));
                $this->redirect(array('action' => 'index'));
            }
            else
            {
                $this->Session->setFlash(__('The activity could not be saved. Please, try again.', true));
            }
        }
        if( empty($this->data) )
        {
            $this->data = $this->Potentialactivity->read(null, $id);
        }
        $potentials = $this->Potentialactivity->Potential->find('list');
        $activitytypes = $this->Potentialactivity->Activitytype->find('list');
        $employees = $this->Potentialactivity->Employee->find('list');
        $assignedtoemployees = $this->Potentialactivity->Employee->find('list');
        $documents = $this->Potentialactivity->Document->find('list');
        $this->set(compact('potentials', 'activitytypes', 'employees', 'assignedtoemployees', 'documents'));
    }

    function admin_delete($id = null)
    {
        if( !$id )
        {
            $this->Session->setFlash(__('Invalid id for activity', true));
            $this->redirect(array('action' => 'index'));
        }
        if( $this->Potentialactivity->delete($id) )
        {
            $this->Session->setFlash(__('Activity deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Activity was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function _sendEmail($title, $recipient, $sender, $subject, $replyto, $attachments, $msgbody)
    {
        Configure::load('emailsettings');
        $this->Email->smtpOptions = array(
            'port' => '26',
            'timeout' => '30',
            'host' => Configure::read('smtp.server'),
            'username' => Configure::read('smtp.username'),
            'password' => Configure::read('smtp.password'),
            'client' => 'ns1761.hostgator.com'
        );
        $this->Email->delivery = 'smtp';

        $this->Email->lineLength = 70;

        $this->Email->to = $recipient;
        $this->Email->subject = $subject;
        $this->Email->replyTo = $replyto;
        $this->Email->from = $sender;
        $this->Email->template = 'default'; // note no '.ctp'
        $this->Email->layout = 'webapp_email';
        //Send as 'html', 'text' or 'both' (default is 'text')
        $this->Email->sendAs = 'both'; // because we like to send pretty mail
        $this->set('title', $title);
        $this->set('msgbody', $msgbody);
        $this->Email->attachments = $attachments;
        //Do not pass any args to send()
        return $this->Email->send();
        $this->set('smtp-errors', $this->Email->smtpError);
    }

}

?>